import { BasicColumn, FormSchema } from '@/components/Table';
import { defHttp } from '@/utils/http/axios';

/**
 * 表格列配置
 */
export const tableColumns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '所属角色',
    dataIndex: 'roleIds',
    component: 'ApiSelect',
    componentProps: {
      api: '/sys/role/select',
    },
  },
  {
    title: '姓名',
    dataIndex: 'realName',
  },
  {
    title: '昵称',
    dataIndex: 'nickName',
  },
  {
    title: '性别',
    dataIndex: 'sex',
    component: 'ApiDict',
    componentProps: {
      code: 'sex',
    },
  },
  {
    title: '是否锁定',
    dataIndex: 'isLocked',
    component: 'ApiDict',
    componentProps: {
      code: 'yes_no',
    },
  },
  {
    title: '手机号',
    dataIndex: 'mobilePhone',
  },
  {
    title: '操作',
    dataIndex: '__ACTION__',
  },
];
/**
 * 搜索表单配置
 */
export const searchFormSchemas: FormSchema[] = [
  {
    label: '用户名',
    field: 'm_LIKE_userName',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '姓名',
    field: 'm_LIKE_realName',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '手机号',
    field: 'm_LIKE_mobilePhone',
    component: 'Input',
    colProps: { span: 6 },
  },
];

/**
 * 新增、编辑表单配置
 */
export const formSchemas: FormSchema[] = [
  {
    field: 'id',
    label: '主键',
    component: 'Input',
    show: false,
    description: false,
  },
  {
    field: 'baseinfo',
    label: '基础信息',
    description: false,
    component: 'Divider',
    colProps: {
      span: 24,
    },
  },
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    componentProps: {
      placeholder: '请输入用户名',
    },
    required: true,
    rules: [
      {
        message: '用户名只能是5-20位字母、数字、下划线',
        pattern: /[0-9A-Za-z_]{5,20}$/,
      },
    ],
    colProps: {
      span: 12,
    },
  },
  {
    field: 'realName',
    label: '姓名',
    component: 'Input',
    componentProps: {
      placeholder: '请输入姓名',
    },
    required: true,
    colProps: {
      span: 12,
    },
  },
  {
    field: 'password',
    label: '密码',
    description: false,
    component: 'InputPassword',
    componentProps: {
      placeholder: '请输入密码',
    },
    ifShow({ values }) {
      return !values.id;
    },
    required: true,
    rules: [
      {
        message: '密码只能是6-20位字母、数字、下划线',
        pattern: /[0-9A-Za-z_]{6,20}$/,
      },
    ],
    colProps: {
      span: 12,
    },
  },
  {
    field: 'confirmPassword',
    label: '确认密码',
    description: false,
    component: 'InputPassword',
    componentProps: {
      placeholder: '请输入确认密码',
    },
    ifShow({ values }) {
      return !values.id;
    },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator(rule, value) {
            if (!value) {
              return Promise.reject('请输入确认密码');
            }
            if (values.password !== value) {
              return Promise.reject('确认密码和密码不一致');
            }
            return Promise.resolve();
          },
        },
      ];
    },
    colProps: {
      span: 12,
    },
  },
  {
    field: 'nickName',
    label: '昵称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入昵称',
    },
    required: false,
    colProps: {
      span: 12,
    },
  },
  {
    field: 'sex',
    label: '性别',
    component: 'ApiDict',
    componentProps: {
      code: 'sex',
      placeholder: '请选择性别',
    },
    required: false,
    colProps: {
      span: 12,
    },
  },
  {
    field: 'email',
    label: '邮箱',
    component: 'Input',
    componentProps: {
      placeholder: '请输入邮箱',
    },
    required: false,
    rules: [
      {
        required: false,
        message: '请输入正确的邮箱',
        // 邮箱正则
        pattern: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+(\.[a-zA-Z0-9_.-]+)+$/,
      },
    ],
    colProps: {
      span: 12,
    },
  },
  {
    field: 'mobilePhone',
    label: '手机号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入手机号',
    },
    required: true,
    rules: [
      {
        message: '请输入正确的手机号',
        // 手机号正则
        pattern: /^1[3456789]\d{9}$/,
      },
    ],
    colProps: {
      span: 12,
    },
  },
  {
    field: 'tel',
    label: '联系电话',
    component: 'Input',
    componentProps: {
      placeholder: '请输入联系电话',
    },
    required: false,
    colProps: {
      span: 12,
    },
  },
  {
    field: 'deptId',
    label: '所属部门',
    component: 'ApiTreeSelect',
    componentProps: {
      api: (params) => {
        return defHttp.post({ url: '/sys/dept/tree', params });
      },
      labelField: 'name',
      valueField: 'id',
      placeholder: '请选择部门',
    },
    required: true,
    colProps: {
      span: 12,
    },
  },
  {
    field: 'postId',
    label: '所属岗位',
    component: 'ApiSelect',
    ifShow: ({ values }) => {
      return values?.sex != 3;
    },
    description({ values }) {
      return values?.sex != 3;
    },
    componentProps({ formModel, schema }) {
      return {
        // immediate: false,
        cacheKey: '/sys/post/select',
        params: {
          includeType: 1,
          includeIds: formModel[schema?.field] ? [formModel[schema?.field]] : undefined,
        },
        api: (params) => {
          return defHttp.post({ url: '/sys/post/select', params });
        },
        placeholder: '请选择岗位',
      };
    },
    required: true,
    colProps: {
      span: 12,
    },
  },
  {
    field: 'otherinifo',
    label: '其他信息',
    description: false,
    component: 'Divider',
    colProps: {
      span: 24,
    },
  },
  {
    field: 'avatar',
    label: '头像',
    component: 'Upload',
    componentProps: {
      multiple: false,
      pictureCard: true,
      isNeedConvert: true,
      resultField: 'url',
      api: () => {
        return defHttp.post({ url: '/sys/fileInfo/upload' });
      },
    },
    required: false,
    colProps: {
      span: 24,
    },
  },
  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
    },
    required: false,
    colProps: {
      span: 24,
    },
  },
];
